package ua.maxicash.util.criteria.handlers;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Junction;
import org.hibernate.criterion.Restrictions;
import ua.maxicash.util.Filter;
import ua.maxicash.util.criteria.RestrictionsWrapper;

import java.util.List;

public class JobTypeCriteriaHandler extends  CriteriaHandler {

    private Filter filter;

    public JobTypeCriteriaHandler(List<String> aliases) {
        super(aliases);
    }

    public JobTypeCriteriaHandler() {
    }

    @Override
    public void setUp(Filter filter) {
        this.filter = filter;
    }

    @Override
    public DetachedCriteria handle(DetachedCriteria criteria, Junction junction) {

        if (!aliases.contains("jobType")) {
            criteria.createAlias("jobType", "jobType");
            aliases.add("jobType");
        }

        junction.add(RestrictionsWrapper.getCriterion(filter.getOp(),
                  "jobType.title",filter.getData()));

        return criteria;
    }
}
